#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <utility>
#include <limits.h>
#include <cmath>
#include <memory>
using namespace std;
using namespace placeholders;
template<class T>
void print(T &t) {
    for (auto &x : t) {
        cout << x << "  ";
    }
    cout << endl;
}

class Figure
{
public:
    //纯虚函数
    virtual void display() const = 0;
    virtual double area() const = 0;
    virtual ~Figure() = default;
};

class Rectangle
    : public Figure
{
public:
    Rectangle(double length = 0, double width = 0)
        : _length(length)
        , _width(width)
    {
        cout << "Rectangle(double = 0, double = 0)" << endl;
    }

    void display() const override
    {
        cout << "Rectangle";
    }

    double area() const override
    {
        return _length * _width;
    }

    ~Rectangle()
    {
        cout << "~Rectangle()" << endl;
    }
private:
    double _length;
    double _width;
};

class Circle
    : public Figure
{
public:
    Circle(double radius = 0)
        : _radius(radius)
    {
        cout << "Circle(double = 0)" << endl;
    }

    void display() const override
    {
        cout << "Circle";
    }

    double area() const override
    {
        return 3.14 * _radius * _radius;;
    }

    ~Circle()
    {
        cout << "~Circle()" << endl;
    }
private:
    double _radius;
};

class Triangle
    : public Figure
{
public:
    Triangle(double a = 0, double b = 0, double c = 0)
        : _a(a)
        , _b(b)
        , _c(c)
    {
        cout << "Triangle(double = 0, double = 0, double = 0)" << endl;
    }

    void display() const override
    {
        cout << "Triangle";
    }

    double area() const override
    {
        //海伦公式
        double tmp = (_a + _b + _c) / 2;

        return sqrt(tmp * (tmp - _a) * (tmp - _b) * (tmp - _c));
    }

    ~Triangle()
    {
        cout << "~Triangle()" << endl;
    }
private:
    double _a;
    double _b;
    double _c;
};

class factory {
public:
    virtual Figure *create() = 0;
    virtual ~factory() = default;
};
class factoryRectangle :public factory{
    Figure *create() override {
        return new Rectangle(2, 3);
    }
    ~factoryRectangle() {
        cout << "~factoryRectangle()" << endl;
    }
};
class factoryCircle :public factory {
    Figure *create() override {
        return new Circle(2);
    }
    ~factoryCircle() {
        cout << "~factoryCircle()" << endl;
    }
};
class factoryTriangle :public factory {
    Figure *create() override {
        return new Triangle(3, 4, 5);
    }
    ~factoryTriangle() {
        cout << "~factoryTriangle()" << endl;
    }
};

void func(Figure *pfig)
{
    pfig->display();
    cout << "的面积 : " << pfig->area() << endl;
}
void test0()
{
    cout << (&"123") << endl;
    cout << (&"123") << endl;
}

void test1()
{
    shared_ptr<factory>fr(new factoryRectangle());
    unique_ptr<Figure>r(fr->create());
    shared_ptr<factory>fc((factory *)new factoryRectangle());
    unique_ptr<Figure>c(fc->create());
    shared_ptr<factory>ft((factory *)new factoryRectangle());
    unique_ptr<Figure>t(ft->create());

    func(r.get());
    func(c.get());
    func(t.get());
    
    //shared_ptr<factory>fpp((factory*)new factoryRectangle());
    //shared_ptr<int> p(new int());
}
int main() {
    //test0();
    test1();
    return 0;
}
